package main

import (
	"fmt"
	"gorule/core"
	"strconv"
)

func main(){
	ctx := core.NewContext()
	ctx.Set("bool", false)
	rule1 := core.Rule(
		func(ctx *core.Context) bool {
			v, _ := strconv.ParseBool(fmt.Sprintf("%v", ctx.Get("bool")))
			return v
		},
		func(ctx *core.Context){
			ctx.Set("name", "niyoufa")
		},
	)
	rule2 := core.Rule(
		func(ctx *core.Context) bool {
			return true
		},
		func(ctx *core.Context){
			ctx.Set("name", "niyoufa")
		},
	)
	rule1.Execute(&ctx)
	fmt.Println("rule1", ctx.Get("name"))
	rule2.Execute(&ctx)
	fmt.Println("rule2", ctx.Get("name"))

	ctx = core.NewContext()
	ctx.Set("bool", false)
	rule3 := core.AndRuleStruct{}
	rule3.AddRule(rule1, rule2)
	rule3.Execute(&ctx)
	fmt.Println("rule3", ctx.Get("name"))

	ctx = core.NewContext()
	ctx.Set("bool", false)
	rule4 := core.OrRuleStruct{}
	rule4.AddRule(rule1, rule2)
	rule4.Execute(&ctx)
	fmt.Println("rule4", ctx.Get("name"))

	ctx = core.NewContext()
	ctx.Set("bool", false)
	rule5 := core.AndRule(&rule3, &rule4)
	rule5.Execute(&ctx)
	fmt.Println("rule5", ctx.Get("name"))

	ctx = core.NewContext()
	ctx.Set("bool", false)
	rule6 := core.OrRule(&rule3, &rule4)
	rule6.Execute(&ctx)
	fmt.Println("rule6", ctx.Get("name"))

	ctx = core.NewContext()
	rule7 := core.NotRule(rule1)
	rule7.Execute(&ctx)
	fmt.Println("rule7", ctx.Get("name"))

	ctx = core.NewContext()
	rule8 := core.NotRule(rule2)
	rule8.Execute(&ctx)
	fmt.Println("rule8", ctx.Get("name"))

	ctx = core.NewContext()
	ctx.Set("bool", false)
	rule9 := core.AndRuleStruct{}
	rule9.AddRule(rule2, &rule4, &rule7)
	rule9.Execute(&ctx)
	fmt.Println("rule9", ctx.Get("name"))

}